package com.example.pkmedical.Activity.weight.weightdevice;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.NumberUtil;
import com.example.pkmedical.Utils.SPUtils;
import com.example.pkmedical.Utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserHeightActivity extends BaseActivity {

    @BindView(R.id.userheight_title_text)
    TextView userheightTitleText;
    @BindView(R.id.userheight_edit)
    EditText userheightEdit;
    @BindView(R.id.userheight_edit_layout)
    RelativeLayout userheightEditLayout;
    @BindView(R.id.userheight_company_left)
    TextView userheightCompanyLeft;
    @BindView(R.id.userheight_company_text)
    TextView userheightCompanyText;
    @BindView(R.id.userheight_company_layout)
    RelativeLayout userheightCompanyLayout;
    @BindView(R.id.userheight_yes_button)
    Button userheightYesButton;
    @BindView(R.id.userheight_no_button)
    Button userheightNoButton;
    private PopupWindow CompanyPop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_height);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        userheightEdit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    }

    @Override
    public void initView() {
        switch (CommandUtil.HEIGHT_COMPANY_TYPE) {
            case 0:
                userheightCompanyText.setText(getString(R.string.cm));
                userheightCompanyLeft.setText(getString(R.string.cm));
                break;
            case 1:
                userheightCompanyText.setText(getString(R.string.yc));
                userheightCompanyLeft.setText(getString(R.string.yc));
                break;
        }
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick({R.id.userheight_edit_layout, R.id.userheight_company_layout, R.id.userheight_yes_button, R.id.userheight_no_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.userheight_edit_layout:

                break;
            case R.id.userheight_company_layout:
                initCefanWindow();
                break;
            case R.id.userheight_yes_button:
                if (NumberUtil.checkStringIsNull(userheightEdit.getText().toString())) {
                    SPUtils.getInstance().put("user_height", userheightEdit.getText().toString());
                    UserHeightActivity.this.finish();
                } else {
                    ToastUtils.show(MyApp.getAppContext(), "请输入正常身高");
                }

                break;
            case R.id.userheight_no_button:
                UserHeightActivity.this.finish();
                break;
        }
    }

    /**
     * 选择器
     */
    private void initCefanWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.item_company, null);
        RelativeLayout yc = view.findViewById(R.id.company_item_inch);
        RelativeLayout cm = view.findViewById(R.id.company_item_cm);
        switch (CommandUtil.HEIGHT_COMPANY_TYPE) {
            case 1:
                yc.setVisibility(View.GONE);
                break;
            case 0:
                cm.setVisibility(View.GONE);
                break;
        }

        yc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userheightCompanyText.setText(R.string.yc);
                CommandUtil.HEIGHT_COMPANY_TYPE = 1;
                userheightCompanyLeft.setText(getString(R.string.yc));
                CompanyPop.dismiss();
            }
        });
        cm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userheightCompanyText.setText(R.string.cm);
                CommandUtil.HEIGHT_COMPANY_TYPE = 0;
                userheightCompanyLeft.setText(getString(R.string.cm));
                CompanyPop.dismiss();
            }
        });
        CompanyPop = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        CompanyPop.setFocusable(true);
        CompanyPop.setOutsideTouchable(true);
        CompanyPop.setBackgroundDrawable(new ColorDrawable());
        CompanyPop.showAsDropDown(userheightCompanyLayout);

    }
}
